﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Scheduler;

namespace SheepClock.CustomUIComponents
{
    public partial class AlarmListItem : UserControl
    {
        int zIndex = 0;
        public event EventHandler DeleteTapped;

        public AlarmListItem()
        {
            InitializeComponent();

            myAlarmItem.editButton.Tap += new EventHandler<GestureEventArgs>(editButton_Tap);
            mySettingsItem.saveButton.Tap += new EventHandler<GestureEventArgs>(saveButton_Tap);
            mySettingsItem.deleteButton.Tap += new EventHandler<GestureEventArgs>(deleteButton_Tap);

            mySettingsItem.hourTextBox.TextChanged += new TextChangedEventHandler(timeTextBox_TextChanged);
            mySettingsItem.minuteTextBox.TextChanged += new TextChangedEventHandler(timeTextBox_TextChanged);
            mySettingsItem.NameTextBox.TextChanged += new TextChangedEventHandler(NameTextBox_TextChanged);

            myAlarmItem.AlarmSwitch.Tap += new EventHandler<GestureEventArgs>(AlarmSwitch_Tap);

            myAlarmItem.SetValue(Canvas.ZIndexProperty, ++zIndex);
            this.Height = 80;
        }

        public int Index
        {
            get;
            set;
        }
        public string AlarmID
        {
            get;
            set;
        }

        void editButton_Tap(object sender, GestureEventArgs e)
        {
            this.Height = 200;
            mySettingsItem.SetValue(Canvas.ZIndexProperty, ++zIndex);

            GoToSettingsStoryBoard.Begin();
        }

        void saveButton_Tap(object sender, GestureEventArgs e)
        {
            this.Height = 80;
            myAlarmItem.SetValue(Canvas.ZIndexProperty, ++zIndex);

            if (myAlarmItem.AlarmSwitch.IsON)
                SetAlarmON();

            BackFromSettingsStoryBoard.Begin();
        }

        private void deleteButton_Tap(object sender, GestureEventArgs e)
        {
            if (DeleteTapped != null)
                DeleteTapped(this, null);
        }

        void NameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            myAlarmItem.AlarmName.Text = mySettingsItem.NameTextBox.Text;
        }

        void timeTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            myAlarmItem.AlarmTime.Text = "@" + mySettingsItem.hourTextBox.Text + ":" + mySettingsItem.minuteTextBox.Text;
        }


        void AlarmSwitch_Tap(object sender, GestureEventArgs e)
        {
            if (myAlarmItem.AlarmSwitch.IsON)
                SetAlarmON();
            else
                SetAlarmOFF();
        }

        public void SetAlarmON()
        {
            if (ScheduledActionService.Find(AlarmID) != null)
                ScheduledActionService.Remove(AlarmID);

            Alarm alarm = new Alarm(AlarmID);
            alarm.Content = myAlarmItem.AlarmName.Text;
            alarm.RecurrenceType = RecurrenceInterval.Daily;

            int hour = Convert.ToInt32(myAlarmItem.AlarmTime.Text.Substring(1).Split(':')[0]);
            int minute = Convert.ToInt32(myAlarmItem.AlarmTime.Text.Substring(1).Split(':')[1]);
            DateTime alarmTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, 0);
            if (alarmTime - DateTime.Now < new TimeSpan(0))
                alarmTime = alarmTime.AddDays(1);
            alarm.BeginTime = alarmTime;

            ScheduledActionService.Add(alarm);
        }
        public void SetAlarmOFF()
        {
            if (ScheduledActionService.Find(AlarmID) != null)
                ScheduledActionService.Remove(AlarmID);
        }

    }
}